package com.example.yan_music.base.fragment;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.yan_music.R;
import com.example.yan_music.base.view.IView;
import com.example.yan_music.event.ReplaceViewPagerFragmentEvent;
import com.example.yan_music.service.PlayerService;
import com.example.yan_music.utils.XLog;
import com.example.yan_music.utils.XToast;
import com.example.yan_music.utils.statusbar.StatusBarUtil;
import com.example.yan_music.widge.dialog.SingleDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 *  Fragment的基类
 */
public class BaseFragment extends Fragment implements IView, View.OnClickListener {

    private SingleDialog mSingleAlertDialog;

    public BaseFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, container, false);
        EventBus.getDefault().register(this);
        return view;
    }


    @Override
    public void showToast(String msg) {
        XToast.show(msg, XToast.Type.POINT);
    }

    /**
     * 显示单体对话框
     * @param dialogRes 对话框的具体布局ID
     */
    @Override
    public void showSingleDialog(int dialogRes) {
        mSingleAlertDialog = new SingleDialog(getContext(), R.style.single_dialog);
        mSingleAlertDialog.setLayoutRes(dialogRes);
        mSingleAlertDialog.show();
    }

    /**
     * 替换Fragment
     * @param fragment 替换的Fragment
     */
    public void replaceFragment(Fragment fragment){
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        // 切换Fragment时执行的动画
        transaction.setCustomAnimations(
                R.anim.fragment_enter,
                R.anim.fragment_exit,
                R.anim.fragment_pop_enter,
                R.anim.fragment_pop_exit);
        // R.id.fragment_container 是 activity_main.xml中 盛装 Fragment的容器
        transaction.add(R.id.vp_fragment_container, fragment);
        transaction.hide(this);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    @Override
    public void onClick(View v) {

    }

    public void init(View view){

    }

    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    /**
     * 返回上一个界面
     */
    private void backToPrevious(Fragment previousFragment){
       //getActivity().getSupportFragmentManager().popBackStack();
        ReplaceViewPagerFragmentEvent event = new ReplaceViewPagerFragmentEvent();
        event.setFragment(previousFragment);
        EventBus.getDefault().post(event);
    }

    /**
     * 处理物理返回键事件
     * @return
     */
    public boolean onBackPressed(){
        return false;
    }

    /**
     * 只修改修改状态栏颜色，不改变文字图标颜色
     * @param statusBarColor    状态栏颜色
     */
    public void modifyStatusBar(int statusBarColor){
        StatusBarUtil.setStatusBarMode(getActivity(), false, statusBarColor);
    }

    /**
     * 同时修改状态栏颜色和文字图标颜色
     * @param isTextDark
     * @param statusBarColor
     */
    public void modifyStatusBar(boolean isTextDark, int statusBarColor){
        StatusBarUtil.setStatusBarMode(getActivity(), true, statusBarColor);
    }

    /**
     * 修改底部导航栏颜色
     * @param navigarionBarColorRes
     */
    public void modifyNavigationBar(int navigarionBarColorRes){
        getActivity().getWindow().setNavigationBarColor(getActivity().getResources().getColor(navigarionBarColorRes));
    }

    /**
     * 返回
     *
     * @param fragment
     */
    public void back(Fragment fragment){
        backToPrevious(fragment);
    }

}